﻿package net.wangit.view.element.component;

import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.support.AbstractElementWriter;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.util.ElementUtil;
import net.wangit.view.util.Html;

public class TextFieldElementWriter extends AbstractElementWriter {

	public TextFieldElementWriter() {
	}

	protected void writeText(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		Html.set(html, ElementUtil.getValueOrDefaultWithFormat(ac, ec, row));
	}

	protected void writeTextWithValue(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		String value = ElementUtil.getValueOrDefaultWithFormat(ac, ec, row);
		Html.set(html, value);
		html.append("<input type=\"hidden\" name=\"").append(
				ElementUtil.getAlias(ec)).append("\"");
		Html.setValue(html, value);
		html.append("/>");
	}

	protected void writeControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		html.append("<input type=\"text\" name=\"").append(
				ElementUtil.getAlias(ec)).append("\"");
		Html.setAttribute(html, ElementUtil.resolveCustom(ac, ec, row));
		Html.setValue(html, ElementUtil
				.getValueOrDefaultWithFormat(ac, ec, row));
		Html.setCss(html, ec.getCss(), "E1");
		Html.setTitle(html, ElementUtil.resolveTitle(ac, ec, row));
		Html.setStyleWithWidth(html, ec.getWidth());
		Html.setMaxLength(html, ec.getMaxLength());
		html.append("/>");
	}

	protected void writeReadOnlyControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		html.append("<input type=\"text\" name=\"").append(
				ElementUtil.getAlias(ec)).append("\"");
		Html.setAttribute(html, ElementUtil.resolveCustom(ac, ec, row));
		Html.setValue(html, ElementUtil
				.getValueOrDefaultWithFormat(ac, ec, row));
		Html.setCss(html, ec.getCss(), "E1D");
		Html.setTitle(html, ElementUtil.resolveTitle(ac, ec, row));
		Html.setStyleWithWidth(html, ec.getWidth());
		html.append(" readonly />");
	}

	protected void writeDisabledControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		html.append("<input type=\"text\" name=\"").append(
				ElementUtil.getAlias(ec)).append("\"");
		Html.setAttribute(html, ElementUtil.resolveCustom(ac, ec, row));
		Html.setValue(html, ElementUtil
				.getValueOrDefaultWithFormat(ac, ec, row));
		Html.setCss(html, ec.getCss(), "E1D");
		Html.setTitle(html, ElementUtil.resolveTitle(ac, ec, row));
		Html.setStyleWithWidth(html, ec.getWidth());
		html.append(" disabled />");
	}

	protected void writeHidden(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		ElementUtil.setHiddenWithFormat(ac, html, ec, row);
	}
}
